package org.yin.hang.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yin.hang.dao.KehuglDao;
import org.yin.hang.pojo.Kehugl;
import org.yin.hang.utils.ResponseResult;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
@CrossOrigin(value = "*")
public class KehuglCotroller {


    @Autowired
    private KehuglDao kehuDao;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<Kehugl>> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, Kehugl condition) throws Exception {
        Map<String,Object> map = new HashMap<>();


        Page<Kehugl> pager = PageHelper.startPage(page, limit);
        List<Kehugl> list = kehuDao.getConditionList(condition);


        return new ResponseResult(pager.getTotal(),list);
    }


    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(int kh_Id) throws Exception{
        kehuDao.delete(kh_Id);
        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/batchDelete")
    @ResponseBody
    public ResponseResult batchDelete(Integer[] kh_Id) throws Exception{
        kehuDao.batchDelete(kh_Id);
        return ResponseResult.createSuccessResult();
    }


//    @RequestMapping("/add")
//    @ResponseBody
//    public ResponseResult add(SysUser sysUser, String roleIds) throws Exception{
//        sysUserDao.add(sysUser);
//        //添加关系
//        String[] array = roleIds.split(",");
//        int[] ids = new int[array.length];
//        for (int i = 0; i < array.length; i++) {
//            ids[i] = Integer.parseInt(array[i]);
//        }
//        sysUserDao.addUserRole(sysUser.getId(),ids);
//        return ResponseResult.createSuccessResult();
//    }


    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(Kehugl kehugl) throws Exception{
        kehuDao.add(kehugl);
        return ResponseResult.createSuccessResult();
    }


    @RequestMapping("/edit")
    @ResponseBody
    public ResponseResult edit(Kehugl kehugl) throws Exception{
        kehuDao.update(kehugl);
        return ResponseResult.createSuccessResult();
    }






}
